<?php
class ControllerCommunityUserGroup extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('community/user_group');

        $this->load->model('community/user_group');

        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_community_user_group->editUserGroups($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['tab_user_ranking'] = $this->language->get('tab_user_ranking');
        $this->data['tab_user_defined_group'] = $this->language->get('tab_user_defined_group');
        $this->data['tab_pre_defined_group'] = $this->language->get('tab_pre_defined_group');

        $this->data['text_ranking_tip'] = $this->language->get('text_ranking_tip');
        $this->data['text_pre_defined_group_tip'] = $this->language->get('text_pre_defined_group_tip');

        $this->data['entry_title'] = $this->language->get('entry_title');
        $this->data['entry_minimum_post'] = $this->language->get('entry_minimum_post');
        $this->data['entry_star_num'] = $this->language->get('entry_star_num');
        $this->data['entry_global_permission'] = $this->language->get('entry_global_permission');
        $this->data['entry_image'] = $this->language->get('entry_image');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_add'] = $this->language->get('button_add');
        $this->data['button_remove'] = $this->language->get('button_remove');
        $this->data['button_edit'] = $this->language->get('button_edit');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['member_rank_title'])) {
            $this->data['error_member_rank_title'] = $this->error['member_rank_title'];
        } else {
            $this->data['error_member_rank_title'] = array();
        }

        if (isset($this->error['member_rank_image'])) {
            $this->data['error_member_rank_image'] = $this->error['member_rank_image'];
        } else {
            $this->data['error_member_rank_image'] = array();
        }

        if (isset($this->error['member_rank_min'])) {
            $this->data['error_member_rank_min'] = $this->error['member_rank_min'];
        } else {
            $this->data['error_member_rank_min'] = array();
        }

        if (isset($this->error['member_rank_stars'])) {
            $this->data['error_member_rank_stars'] = $this->error['member_rank_stars'];
        } else {
            $this->data['error_member_rank_stars'] = array();
        }

        if (isset($this->error['pre_defined_group_title'])) {
            $this->data['error_pre_defined_group_title'] = $this->error['pre_defined_group_title'];
        } else {
            $this->data['error_pre_defined_group_title'] = array();
        }

        if (isset($this->error['pre_defined_group_image'])) {
            $this->data['error_pre_defined_group_image'] = $this->error['pre_defined_group_image'];
        } else {
            $this->data['error_pre_defined_group_image'] = array();
        }

        if (isset($this->error['user_defined_group_title'])) {
            $this->data['error_user_defined_group_title'] = $this->error['user_defined_group_title'];
        } else {
            $this->data['error_user_defined_group_title'] = array();
        }

        if (isset($this->error['user_defined_group_image'])) {
            $this->data['error_user_defined_group_image'] = $this->error['user_defined_group_image'];
        } else {
            $this->data['error_user_defined_group_image'] = array();
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/user_group', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['cancel'] = $this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL');

        $this->load->model('localisation/language');

        $this->data['languages'] = $this->model_localisation_language->getLanguages();

        if (isset($this->request->post['member-rank'])) {
            $this->data['member_ranks'] = $this->request->post['member-rank'];
        } else {
            $this->data['member_ranks'] = $this->model_community_user_group->getMemberRanks();
        }

        if (isset($this->request->post['user-defined-group'])) {
            $this->data['user_defined_groups'] = $this->request->post['user-defined-group'];
        } else {
            $this->data['user_defined_groups'] = $this->model_community_user_group->getUserdefinedRoles();
        }

        if (isset($this->request->post['pre-defined-group'])) {
            $this->data['pre_defined_groups'] = $this->request->post['pre-defined-group'];
        } else {
            $this->data['pre_defined_groups'] = $this->model_community_user_group->getPredefinedRoles();
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $this->template = 'community/user_group.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function permission(){
        $this->load->language('community/global_permission');

        $this->load->model('community/user_group');

        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_community_user_group->editUserGlobalPermission($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/user_group', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $permission=$this->model_community_user_group->getUserGlobalPermission($this->request->get);

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_user_title'] = sprintf($this->language->get('text_user_title'),$permission['title']);

        $this->data['entry_member_ban'] = $this->language->get('entry_member_ban');
        $this->data['entry_member_warn'] = $this->language->get('entry_member_warn');
        $this->data['entry_disallow_posting'] = $this->language->get('entry_disallow_posting');
        $this->data['entry_posting_captcha'] = $this->language->get('entry_posting_captcha');
        $this->data['entry_ignore_flood_limit'] = $this->language->get('entry_ignore_flood_limit');
        $this->data['entry_sig_status'] = $this->language->get('entry_sig_status');
        $this->data['entry_sig_bbcode_status'] = $this->language->get('entry_sig_bbcode_status');
        $this->data['entry_sig_bbcode_img_status'] = $this->language->get('entry_sig_bbcode_img_status');
        $this->data['entry_sig_bbcode_flash_status'] = $this->language->get('entry_sig_bbcode_flash_status');
        $this->data['entry_sig_bbcode_url_status'] = $this->language->get('entry_sig_bbcode_url_status');
        $this->data['entry_sig_bbcode_smile_status'] = $this->language->get('entry_sig_bbcode_smile_status');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_user_group'),
            'href'      => $this->url->link('community/user_group', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/user_group/permission', 'token=' . $this->session->data['token'] . '&rank_id=' . $this->request->get['rank_id'] . '&role_id=' . $this->request->get['role_id'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['cancel'] = $this->url->link('community/user_group', 'token=' . $this->session->data['token'], 'SSL');

        if (isset($this->request->post['permission'])) {
            $this->data['permission'] = $this->request->post['permission'];
        } else {
            $this->data['permission'] = $permission;
        }

        $this->template = 'community/global_permission.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'community/user_group')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (isset($this->request->post['member-rank'])) {
            $previous_min=0;
            $previous_stars=0;

            foreach($this->request->post['member-rank'] as $row_id => $member_rank){
                foreach ($member_rank['rank_title'] as $language_id => $rank_title) {
                    if ((utf8_strlen($rank_title) < 1) || (utf8_strlen($rank_title) > 100)) {
                        $this->error['member_rank_title'][$row_id][$language_id] = $this->language->get('error_rank_title');
                    }
                }

                foreach ($member_rank['rank_image'] as $language_id => $rank_image) {
                    if (utf8_strlen($rank_image) > 255) {
                        $this->error['member_rank_image'][$row_id][$language_id] = $this->language->get('error_rank_image');
                    }
                }

                if($member_rank['rank_min']<$previous_min){
                    $this->error['member_rank_min'][$row_id] = $this->language->get('error_rank_min');
                }

                $previous_min=$member_rank['rank_min'];

                if($member_rank['rank_stars']<$previous_stars){
                    $this->error['member_rank_stars'][$row_id] = $this->language->get('error_rank_stars');
                }

                $previous_stars=$member_rank['rank_stars'];
            }
        }

        if (isset($this->request->post['user-defined-group'])) {
            foreach($this->request->post['user-defined-group'] as $row_id => $user_defined_group){
                foreach ($user_defined_group['role_title'] as $language_id => $role_title) {
                    if ((utf8_strlen($role_title) < 1) || (utf8_strlen($role_title) > 100)) {
                        $this->error['user_defined_group_title'][$row_id][$language_id] = $this->language->get('error_group_title');
                    }
                }

                foreach ($user_defined_group['role_image'] as $language_id => $role_image) {
                    if (utf8_strlen($role_image) > 255) {
                        $this->error['user_defined_group_image'][$row_id][$language_id] = $this->language->get('error_group_image');
                    }
                }
            }
        }
        
        if (isset($this->request->post['pre-defined-group'])) {
            foreach($this->request->post['pre-defined-group'] as $row_id => $pre_defined_group){
                foreach ($pre_defined_group['role_title'] as $language_id => $role_title) {
                    if ((utf8_strlen($role_title) < 1) || (utf8_strlen($role_title) > 100)) {
                        $this->error['pre_defined_group_title'][$row_id][$language_id] = $this->language->get('error_group_title');
                    }
                }

                foreach ($pre_defined_group['role_image'] as $language_id => $role_image) {
                    if (utf8_strlen($role_image) > 255) {
                        $this->error['pre_defined_group_image'][$row_id][$language_id] = $this->language->get('error_group_image');
                    }
                }
            }
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
?>